﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace EventoForms {
    public partial class Form1 : Form {

        ErrorProvider ep = new ErrorProvider();
        ErrorProvider epG = new ErrorProvider();
        NwindDataSet ds;
        BindingSource bs;
        public Form1() {
            InitializeComponent();
             ds = new NwindDataSet();
            NwindDataSetTableAdapters.CustomersTableAdapter cta = new NwindDataSetTableAdapters.CustomersTableAdapter();
            cta.Fill(ds.Customers);
             bs = new BindingSource(ds, "Customers");
            textBox1.DataBindings.Add("Text", bs, "CustomerID");
            bs.MoveNext();
            dataGridView1.DataSource = bs;
            //dataGridView1.DataSource = ds;
            //dataGridView1.DataMember = "Customers";
            epG.ContainerControl = this;
            epG.DataSource = ds;
            epG.DataMember = "Customers";


        }

        private int miUC1_Me() {
         //   MessageBox.Show("asfsadf");
            return default(int);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {
      //      MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
            Brush b = Brushes.Red;

            if (e.RowIndex == -1) {
                e.Graphics.FillRectangle(Brushes.Green, e.CellBounds);
                e.Graphics.FillRectangle(Brushes.DarkGray, e.CellBounds.X - 1, e.CellBounds.Y - 1, e.CellBounds.Width - 1, e.CellBounds.Height - 1);
                if (e.Value != null)
                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.White, e.CellBounds.X, e.CellBounds.Y);
            
                //     e.Graphics.DrawString(e.Value.ToString(), SystemFonts.CaptionFont, Brushes.White, e.CellBounds.X, e.CellBounds.Y);
            } else {
                e.Graphics.FillRectangle(b, e.CellBounds);
                e.Graphics.FillRectangle(Brushes.DarkGray, e.CellBounds.X - 1, e.CellBounds.Y - 1, e.CellBounds.Width - 1, e.CellBounds.Height - 1);
                if (e.Value != null)
                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.White, e.CellBounds.X, e.CellBounds.Y);
            
            }
               
           // e.Handled = true;
        }

        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {

            if (dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "CompanyName") {
                if (e.FormattedValue.Equals("PDVSA")) {
                    e.Cancel=true;
                    ds.Customers.Rows[e.RowIndex].RowError = "Mi Error";
                    ds.Customers.Rows[e.RowIndex].SetColumnError("CompanyName", "El nombre no puede ser vacio");
                }
            }

           


        }

        private void textBox1_Validating(object sender, CancelEventArgs e) {
            if ( ((TextBox)sender).Text == "PDVSA") {

                ds.Customers.Rows[bs.Position].RowError = "Error en Textbox";
                ds.Customers.Rows[bs.Position].SetColumnError("CustomerID", "eRRORRRRR");
                
                
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
        }

        private void Boton_Click(object sender, EventArgs e) {

            Form1 ww = new Form1();
            ww.Show();
            aux = "x";

            MessageBox.Show(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
            System.Threading.Thread th = new System.Threading.Thread(mm);
            th.Start("A");
            //th.Abort();

            System.Threading.Thread th2 = new System.Threading.Thread(mm2);
            th2.Start("B");

        }


        private delegate void md(object str);
        private object aux;



        private void mm(object str) {
            
            lock (aux) {
                MessageBox.Show("BLOQUEADO 1 /" + aux);
                aux = str;
             }

            }

        private void mm2(object str) {
            
            lock (aux) {
                MessageBox.Show("BLOQUEADO 2 /" + aux);
                aux = str;
            }
            
        }

        private void Form1_Load(object sender, EventArgs e) {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");
        }

    }
}
